<?php
/***************************************************************************
 *   copyright				: (C) 2008, 2009 WeBid
 *   site					: http://www.webidsupport.com/
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version. Although none of the code may be
 *   sold. If you have been sold this script, get a refund.
 ***************************************************************************/

if (!defined('InWeBid')) exit();

include $include_path . 'maintainance.php';
include $include_path . 'banners.inc.php';
if (basename($_SERVER['PHP_SELF']) != 'error.php')
	include $include_path . 'stats.inc.php';
include 'includes/SxGeo.php';

$jsfiles = 'js/jquery.js;js/jquery.lightbox.js;';
$jsfiles .= (basename($_SERVER['PHP_SELF']) == 'sell.php') ? ';js/calendar.php' : '';


function getRealIpAddr()
{
  if (!empty($_SERVER['HTTP_CLIENT_IP']))
  {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
  {
    $ip=$_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}


function detect_city($ip) {
	$default = 'Russia';
	return $default;
	/*
	if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
		$ip = '8.8.8.8';

	$curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';

	$url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
	$ch = curl_init();

	$curl_opt = array(
		CURLOPT_FOLLOWLOCATION  => 1,
		CURLOPT_HEADER      => 0,
		CURLOPT_RETURNTRANSFER  => 1,
		CURLOPT_USERAGENT   => $curlopt_useragent,
		CURLOPT_URL       => $url,
		CURLOPT_TIMEOUT         => 1,
		CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],
	);

	curl_setopt_array($ch, $curl_opt);

	$content = curl_exec($ch);

	if (!is_null($curl_info)) {
		$curl_info = curl_getinfo($ch);
	}

	curl_close($ch);

	if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) )  {
		$city = $regs[1];
	}
	if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) )  {
		$state = $regs[1];
	}

	if( $city!='' && $state!='' ){
	  //if ($city != $state)
	  //	$location = $city . ', ' . $state;
	  //else
	  $location = $city;
	  return $location;
	}else{
	  return $default;
	}
	*/
}

global  $_COOKIE, $STARTED_FROM;

if ($user->logged_in) {
	if ((empty($_SESSION['LOGGED_VP_GLOBAL'])) || (empty($_COOKIE['geolocation_place']))){
		$query = "SELECT * FROM " . $DBPrefix . "users_regions WHERE user_id = " . $user->user_data['id'];
		
		$result = mysql_query($query);
		$system->check_mysql($result, $query, __LINE__, __FILE__);
		$req = mysql_fetch_assoc($result);

		$_SESSION['LOGGED_VP_GLOBAL'] = '(('.$req['sw_lat'].','.$req['sw_lng'].'),('.$req['ne_lat'].','.$req['ne_lng'].'))';
		$_SESSION['LOGGED_GEOINFO'] = $req['formatted_addr'];

		setcookie("geolocation_location", "(0,0)",time()+60*60*24*30*12);
		setcookie("geolocation_place", $_SESSION['LOGGED_GEOINFO'],time()+60*60*24*30*12);
		setcookie("geolocation_viewport", $_SESSION['LOGGED_VP_GLOBAL'],time()+60*60*24*30*12);

		$template->assign_vars(array('GEOINFO' => '<a href="#" id="regSel" onclick="SelectUserRegion(\'regSel\',1); return false;">'.$_SESSION['LOGGED_GEOINFO'].'</a>',
									 'PREF_GEOINFO_REG' => 'regSel1_',
									 'VP_GLOBAL' => '<div id="VP_GLOBAL" style="display:none;">'.$_SESSION['LOGGED_VP_GLOBAL'].'</div>'));
	
		$location = "'(0,0)'";
		$viewport = "'".$_SESSION['LOGGED_VP_GLOBAL']."'";
			
	} else {

		$template->assign_vars(array('GEOINFO' => '<a href="#" id="regSel" onclick="SelectUserRegion(\'regSel\',1); return false;">'.$_COOKIE['geolocation_place'].'</a>',
									 'PREF_GEOINFO_REG' => 'regSel1_',
									 'VP_GLOBAL' => '<div id="VP_GLOBAL" style="display:none;">'.$_COOKIE['geolocation_viewport'].'</div>'));
	
		$location = "'".$_COOKIE['geolocation_location']."'";
		$viewport = "'".$_COOKIE['geolocation_viewport']."'";
		//$_SESSION['LOGGED_VP_GLOBAL'] = $viewport;
	}

	
} else {
	 $_SESSION['LOGGED_VP_GLOBAL'] = '';
	 $_SESSION['LOGGED_GEOINFO'] = '';
	 //echo "LOGGED_VP_GLOBAL Очищен!";
	if (!isset($_COOKIE['geolocation_viewport']))
	{
		//$ip=$_SERVER['REMOTE_ADDR'];
		//$ip='91.198.33.1'; //debug
		$ip=getRealIpAddr();
		///$SxGeo = new SxGeo('includes/SxGeoCity.dat', SXGEO_BATCH | SXGEO_MEMORY);
		///$geoinfo = $SxGeo->get($ip);
		$geoinfo = array();
		$geoinfo['city'] = detect_city($ip);
		$geoinfo['lat'] = 0;
		$geoinfo['lon'] = 0;

		$location = "(".$geoinfo['lat'].",". $geoinfo['lon'].")";

		$viewport = "'init'";
		setcookie("geolocation_place", $geoinfo['city'],time()+60*60*24*30*365);
		setcookie("geolocation_location", $location,time()+60*60*24*30*365);

		$location = "'".$location."'";

		$template->assign_vars(array('GEOINFO' => '<a href="#" id="regSel" onclick="SelectUserRegion(\'regSel\',1); return false;">'.$geoinfo['city'].'</a>',
									 'GEOINFO_REG' => '<a href="#" id="regSel1" onclick="SelectUserRegion(\'regSel1\',0); return false;">'.$MSG['77_0020'].'</a>',
									 'PREF_GEOINFO_REG' => 'regSel1_'
									 ));
	} else {
		$template->assign_vars(array('GEOINFO' => '<a href="#" id="regSel" onclick="SelectUserRegion(\'regSel\',1); return false;">'.$_COOKIE['geolocation_place'].'</a>',
									 'GEOINFO_REG' => '<a href="#" id="regSel1" onclick="SelectUserRegion(\'regSel1\',0); return false;">'.$MSG['77_0020'].'</a>',
									 'PREF_GEOINFO_REG' => 'regSel1_'
									 ));
		$location = "'".$_COOKIE['geolocation_location']."'";
		$viewport = "'".$_COOKIE['geolocation_viewport']."'";
	}
} 


if ($STARTED_FROM == 'SELL_PHP_EDIT') {
	if ($map_posted == ',,') {
		$template->assign_vars(array('CONTAINS_MAP' => true,
								   'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,'.$viewport.',false,null,true)"'));
	} else {
		$location = "'(".$map_posted.")'";
		$template->assign_vars(array('CONTAINS_MAP' => true,
								'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,null,true,null,true)"'));
	}

} elseif ($STARTED_FROM == 'SELL_PHP_VIEW') {
  $location = "'(".$map_posted.")'";
	$template->assign_vars(array('CONTAINS_MAP' => true,
                               'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,null,true,null,false)"'));
} elseif ($STARTED_FROM == 'ITEM_PHP') {
  if ($map_posted) {
    $location = "'(".$map_posted.")'";
  	$template->assign_vars(array('CONTAINS_MAP' => true,
                                 'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,null,true,null,false)"'));

  }
} elseif ($STARTED_FROM == 'SELL_PHP_NEW') {
	$template->assign_vars(array('CONTAINS_MAP' => true,
                               'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,'.$viewport.',false,null,true)"'));
} elseif ($STARTED_FROM == 'BROWSE_PHP') {
		global $M_TYPE_POINTS;
		if (isset($M_TYPE_POINTS))
			$template->assign_vars(array('CONTAINS_MAP' => true,
					'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,'.$viewport.',false,true,false,'.$M_TYPE_POINTS.')"'));
	
} elseif ($STARTED_FROM == 'INDEX_PHP') /* or ($STARTED_FROM == 'REGISTER_PHP')*/ {
	$template->assign_vars(array('CONTAINS_MAP' => true,
                               'MAP_INIT'     => 'onload="initialize_map1(\'regSel\','.$location.',null,'.$viewport.',false,true,false)"'));
}





// Get users and auctions counters
$counters = load_counters();

$page_title = (isset($page_title)) ? ' ' . $page_title : '';

$sslurl = $system->SETTINGS['siteurl'];
if ($system->SETTINGS['https'] == 'y')
{
	$sslurl = (!empty($system->SETTINGS['https_url'])) ? $system->SETTINGS['https_url'] : str_replace('http://', 'https://', $system->SETTINGS['siteurl']);
}
// for images/ccs/javascript etc on secure pages
$incurl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') ? $system->SETTINGS['siteurl'] : $sslurl;

$NEW_MESS = '';
if ($user->logged_in) {
	$reminders = get_reminders($user->user_data['id']);
	$NEW_MESS = $reminders[0] + $reminders[1] + $reminders[2];
	$NEW_MESS = ($NEW_MESS > 0) ? '(' . $NEW_MESS . ')' : '';
}



$template->assign_vars(array(
		'DOCDIR' => $DOCDIR, // Set document direction (set in includes/messages.XX.inc.php) ltr/rtl
		'THEME' => $system->SETTINGS['theme'],
		'PAGE_TITLE' => $system->SETTINGS['sitename'] . $page_title,
		'CHARSET' => $CHARSET,
		'DESCRIPTION' => stripslashes($system->SETTINGS['descriptiontag']),
		'KEYWORDS' => stripslashes($system->SETTINGS['keywordstag']),
		'JSFILES' => $jsfiles,
		'LOADCKEDITOR' => (basename($_SERVER['PHP_SELF']) == 'sell.php'),
		'ACTUALDATE' => ActualDate(),
		'LOGO' => ($system->SETTINGS['logo']) ? '<a href="' . $system->SETTINGS['siteurl'] . 'index.php?"><img src="' . $incurl . 'themes/' . $system->SETTINGS['theme'] . '/' . $system->SETTINGS['logo'] . '" border="0" alt="' . $system->SETTINGS['sitename'] . '"></a>' : '&nbsp;',
		'BANNER' => ($system->SETTINGS['banners'] == 1) ? view() : '',
		'HEADERCOUNTER' => $counters,
		'SITEURL' => $system->SETTINGS['siteurl'],
		'SSLURL' => $sslurl,
		'ASSLURL' => ($system->SETTINGS['https'] == 'y' && $system->SETTINGS['usersauth'] == 'y') ? $sslurl : $system->SETTINGS['siteurl'],
		'INCURL' => $incurl,
		'Q' => (isset($q)) ? $q : '',
		'SELECTION_BOX' => file_get_contents($main_path . 'language/' . $language . '/categories_select_box.inc.php'),
		'YOURUSERNAME' => ($user->logged_in) ? $user->user_data['nick'] : '',
		'YOURUSERNAME1' => ($user->logged_in) ? $user->user_data['nick'] . $NEW_MESS : '',
		'B_CAN_SELL' => ($user->can_sell || !$user->logged_in),
		'B_LOGGED_IN' => $user->logged_in,
		'B_BOARDS' => ($system->SETTINGS['boards'] == 'y')
		));

$template->set_filenames(array(
		'header' => 'global_header.tpl'
		));
$template->display('header');
?>